package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsTranOutUnsubmitQueryCmd;
import com.jackrain.nea.fc.dl.mapper.WmsDLTranMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsTranOutUnsubmitQueryCmdImpl extends CommandAdapter implements WmsTranOutUnsubmitQueryCmd {


    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        WmsDLTranMapper wmsDLTranMapper = ApplicationContextHandle.getBean(WmsDLTranMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("WmsTranOutUnsubmitQueryCmdImpl-->param-->{}",param);
//        log.debug("----------入库通知单取消提交查询参数----------" + param);
        JSONArray dataArray = param.getJSONArray("IDS");
        JSONArray fixArray = new JSONArray();

        for (int i = 0; i < dataArray.size(); i++) {

            JSONObject fixcolumn = new JSONObject();
            String idStr = dataArray.getString(i);
            HashMap mainMap = wmsDLTranMapper.selectwmstranoutbillnosql(idStr);
            JSONObject mainJo = new JSONObject(mainMap);
            fixcolumn.put("mainData", mainJo);
            fixArray.add(fixcolumn);

        }
        vh.put("data", fixArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("成功"));
        return vh;

    }

}
